package com.excel.common;

import com.excel.exception.ExcelException;
import com.excel.util.DateTools;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import net.sf.cglib.beans.BeanMap;

public class AnalyticalData implements Constant {
    public AnalyticalData() {
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static void copyProperties(Object target, Object source, String[] properties) {
        BeanMap targetBeanMap = BeanMap.create(target);
        BeanMap srcBeanMap = BeanMap.create(source);
        Set<Entry> propertySet = srcBeanMap.entrySet();
        Object property = null;
        Iterator var8 = propertySet.iterator();

        while(true) {
            while(true) {
                while(var8.hasNext()) {
                    Entry entry = (Entry)var8.next();
                    property = entry.getKey();
                    if (properties != null && properties.length > 0) {
                        String[] var12 = properties;
                        int var11 = properties.length;

                        for(int var10 = 0; var10 < var11; ++var10) {
                            String propertyName = var12[var10];
                            if (property.equals(propertyName) && targetBeanMap.containsKey(property)) {
                                targetBeanMap.put(property, entry.getValue());
                                break;
                            }
                        }
                    } else if (targetBeanMap.containsKey(property)) {
                        targetBeanMap.put(property, entry.getValue());
                    }
                }

                return;
            }
        }
    }

    public static <T> T createBean(Class<T> clazz, Map<String, Object> srcMap, String[] tarProperties, String[] srcProperties) throws InstantiationException, IllegalAccessException {
        T t = clazz.newInstance();
        BeanMap beanMap = BeanMap.create(t);
        if (tarProperties != null && tarProperties.length > 0 && srcProperties != null && srcProperties.length > 0) {
            int index = 0;
            String[] var10 = tarProperties;
            int var9 = tarProperties.length;

            for(int var8 = 0; var8 < var9; ++var8) {
                String tarProperty = var10[var8];
                if (!tarProperty.contains(".")) {
                    Object val = srcMap.get(srcProperties[index++]);
                    beanMap.put(tarProperty, val);
                } else {
                    String[] _tarProperties = tarProperty.split("\\.");
                    int _index = 0;
                    Object _obj = t;

                    do {
                        _obj = BeanMap.create(_obj).get(_tarProperties[_index++]);
                    } while(_index < _tarProperties.length - 1);

                    BeanMap.create(_obj).put(_tarProperties[_index], srcMap.get(srcProperties[index++]));
                }
            }
        }

        return t;
    }

    public static String analyFieldType(Object title) {
        String val = "";
        if (title instanceof Integer) {
            val = ((Integer)title).toString();
        } else if (title instanceof Date) {
            val = DateTools.format((Date)title);
        } else if (title instanceof Long) {
            val = ((Long)title).toString();
        } else if (title instanceof Double) {
            val = ((Double)title).toString();
        } else if (title instanceof Double) {
            val = ((Double)title).toString();
        } else if (title instanceof String) {
            val = title.toString();
        }

        return val;
    }

    public static Object getAttribute(Class<?> className, String parameter, String value) {
        try {
            Field filed = className.getDeclaredField(parameter);
            if (filed != null) {
                String tyep = filed.getType().toString();
                if ("int".equals(tyep) || "class java.lang.Integer".equals(tyep)) {
                    return value != null && !"".equals(value) ? Integer.parseInt(value) : null;
                }

                if ("shot".equals(tyep) || "class java.lang.Short".equals(tyep)) {
                    return value != null && !"".equals(value) ? Short.parseShort(value) : null;
                }

                if ("long".equals(tyep) || "class java.lang.Long".equals(tyep)) {
                    return value != null && !"".equals(value) ? Long.parseLong(value) : null;
                }

                if ("float".equals(tyep) || "class java.lang.Float".equals(tyep)) {
                    return value != null && !"".equals(value) ? Float.parseFloat(value) : null;
                }

                if ("double".equals(tyep) || "class java.lang.Double".equals(tyep)) {
                    return value != null && !"".equals(value) ? Double.parseDouble(value) : null;
                }

                if ("class java.util.Date".equals(tyep)) {
                    return value != null && !"".equals(value) ? DateTools.format(value) : null;
                }
            }

            return value;
        } catch (Exception var5) {
            throw new ExcelException("getAttribute method Exception :" + var5.getMessage());
        }
    }
}
